﻿var indicationCreateVM = {
    PatientID: ko.observable(),
    PatientName: ko.observable(),
    IndicationInformation: ko.observable(),
    loadPatientInfo: function (indexController) {
        indexController.enableBtnCreateIndicationSave(false);
        this.PatientID(patientVM_Doctor.Patient()[0].ID);
        this.PatientName(patientVM_Doctor.Patient()[0].Name);
        this.IndicationInformation("");
        indexController.enableBtnCreateIndicationSave(true);
    },
    saveIndication: function (indexController) {
        var self = this;
        indexController.enableBtnCreateIndicationSave(false);
        indexController.enableBtnCreateIndicationCancel(false);
        indexController.message.processing("saving");
        $.ajax({
            type: 'post',
            url: home + 'Doctor/CreateIndication',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: ko.toJSON(this),
            success: function (data) {
                //close update indication form
                indexController.displayCreateIndicationForm(false);
                indexController.message.processComplete("save_success");
                //remove old data, add new data to list of indications
                //patientVM_Doctor.Indications.unshift(data[0]);
                //patientVM_Doctor.sortIndicationsDESC;
                patientVM_Doctor.Indications.removeAll();
                patientVM_Doctor.Indications(data[0]);
                patientVM_Doctor.IndicationsPage(data[1].PageNumber);
                patientVM_Doctor.IndicationsPageCount(data[1].PageCount);
                indexController.enableIndicationPreviousGroup(false);
                indexController.enableIndicationNextGroup(false);
                patientVM_Doctor.IndicationsPageNavHandling(indexController);
            },
            error: function (err) {
                if (err.status == 200) {
                    indexController.message.processComplete(err.responseText);
                }
                else {
                    indexController.message.processComplete("save_fail");
                }
            },
            complete: function (xhr, textStatus) {
                indexController.enableBtnCreateIndicationSave(true);
                indexController.enableBtnCreateIndicationCancel(true);
            }
        });
    }
};